<template>
  <el-dialog
    title="选择上级推荐人"
    :visible.sync="show"
    width="800px"
    append-to-body
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item prop="sysUserid">
            <el-input
              v-model="queryParams.sysUserid"
              placeholder="请输入会员ID"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="0">
          <el-form-item prop="key">
            <el-input
              v-model="queryParams.key"
              placeholder="请输入会员昵称/手机号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="" label-width="0">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="ID" align="center" prop="sysUserid">
        <template slot-scope="{ row }">
          <el-radio v-model="radioVal" :label="row.sysUserid"></el-radio>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatar" widht="100">
        <template slot-scope="{ row }">
          <div class="avatar">
            <image-preview
              :src="row.avatar"
              height="50"
              width="50"
            ></image-preview>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机" align="center" prop="phonenumber" />
      <el-table-column label="会员等级" align="center" prop="memberName" />
      <!-- <el-table-column label="已绑定推荐人" align="center" prop="" /> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { selectPageList } from "@/api/system/member/rule";

export default {
  name: "ChooseReferrer",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysUserid: null,
        key: null,
      },
      // 表格数据
      dataList: [],
      // 总条数
      total: 0,
      // 单选
      radioVal: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      selectPageList(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    confirm() {
      if (this.radioVal != null) {
        let referrer = this.dataList.find(
          (item) => item.sysUserid == this.radioVal
        );
        this.$emit("confirm", {
          avatar: referrer.avatar,
          sysUserid: referrer.sysUserid,
          nickName: referrer.nickName,
        });
        this.radioVal = null;
      } else {
        this.$message.error("请选择推荐人");
      }
    },
    cancel() {
      this.$emit("close");
    },
  },
};
</script>
<style scoped>
.avatar {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
